package com.bilubi.reggie.reggie.controller;


import com.bilubi.reggie.reggie.bean.Category;
import com.bilubi.reggie.reggie.common.R;
import com.bilubi.reggie.reggie.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public R<PageInfo<Category>> findAll(@RequestParam(value = "page") Integer page,
                                     @RequestParam(value = "pageSize") Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Category> all = categoryService.findAll();
        PageInfo<Category> pageInfo = new PageInfo<>(all);
        return R.success(pageInfo);
    }

    @RequestMapping(method = RequestMethod.POST)
    public R<String> addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return R.success();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public R<String> updateCategory(@RequestBody Category category) {
        categoryService.updateCategoryById(category);
        return R.success();
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public R<String> deleteCategoryById(@RequestParam(value = "ids") Long id) {
        categoryService.deleteCategoryById(id);
        return R.success();
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<Category>> findAllCategory(@RequestParam(value = "type", required = false) Integer type) {
        log.info("type:{}", type);
        List<Category> all = categoryService.findAllByType(type);
        return R.success(all);
    }

}
